//gulp 的入口文件
//在最新的gulp中约定每个任务都是异步任务，因此我们需要标记任务完成
exports.foo = done =>{
  console.log('foo task working~');

  done() //标识任务完成
}

exports.default = done=>{
  console.log("default task working~");
  done()
}

//gulp 4.0以前注册一个任务通过,不推荐，推荐上面导出函数成员方式
const gulp = require('gulp')
gulp.task('bar',done=>{
  console.log('bar working~');
  done()
})

console.log("---------------------------------------------------------------");
//组合任务
//例如编译css和js，他们是互不干扰的，这两个任务可以并行处理，提高构建效率
//例如部署，部署的任务需要先执行编译任务，需要通过串行执行任务
const {series,parallel} = require('gulp')

const task1 = done =>{
  setTimeout(()=>{
    console.log('task1 working~');
    done()
  },1000)
}

const task2 = done =>{
  setTimeout(()=>{
    console.log('task2 working~');
    done()
  },1000)
}
const task3 = done =>{
  setTimeout(()=>{
    console.log('task3 working~');
    done()
  },1000)
}

exports.foo = series(task1,task2,task3) //按顺序串行依次执行

exports.bar = parallel(task1,task2,task3) //并行执行

console.log("---------------------------------------------------------------");

//异步任务（三种方式）

//回调函数
exports.callback = done=>{ 
  console.log('callback task~');
  done()
}

//如果多个任务执行，后面不会再执行
exports.callback_error = done=>{
  console.log('callback_error task~');
  done(new Error('task Failed'))
}

//promise
exports.promise = ()=>{
  console.log('promise task~');
  return Promise.resolve() //要返回一个promise对象
}

exports.promise_error = ()=>{
  console.log('promise_error task~');
  return Promise.reject(new Error('task Failed')) //要返回一个promise对象
}

//async await node8以上
const timeout = time=>{
  return new Promise(resolve=>{
    setTimeout(resolve,time)
  })
}
exports.async = async ()=>{
  await timeout(1000)
  console.log('async task~');
}

//stream流的方式
const fs = require('fs')
// exports.stream = ()=>{
//   const readStream = fs.createReadStream('package.json') 
//   const writeStream = fs.createWriteStream('temp.txt') //写入流
//   readStream.pipe(writeStream) //read通过pipe方式导入write中,类似于文件复制
//   return readStream //结束的时机就是readStream END的时候
// }


exports.stream = done=>{
  const readStream = fs.createReadStream('package.json') 
  const writeStream = fs.createWriteStream('temp.txt') //写入流
  readStream.pipe(writeStream)
  readStream.on('end',()=>{ //实质上是监听end事件
    done()
  })
}

//Gulp构建过程核心工作原理
//读出文件，转化，写入文件